<template>
	<div>
   <el-button type="primary" @click="handleAdd" style="float: right; margin-top: -55px;" size="mini">督导巡查</el-button>
		<!-- 添加或修改安全检查单对话框 -->
    <el-dialog :title="title" center :visible.sync="open" width="720px" @close="close" append-to-body>
      <el-tabs v-model="activeName">
        <el-tab-pane label="流程表单" name="first">
          <AddTab @updateForm="updateForm" ref="addTab" :form="form"></AddTab>
        </el-tab-pane>
        <el-tab-pane v-if="form.instanceId" label="检查指标" name="second">
          <ItemTab ref="itemTab" :data="form.labSafetyCheckIndexList" :instanceId="form.instanceId"></ItemTab>
        </el-tab-pane>
        <el-tab-pane label="流程导入" name="third" v-if="form.instanceId">
          <FlowChoose ref="workFlow" @workflow="setWorkflow" :businessType="form.businessTypeOpt" :applyScope="form.applyScope"></FlowChoose>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="activeName=='first' || activeName=='second' ">保存</el-button>
        <el-button type="primary" @click="submitWorkflow" v-if="activeName=='third'">提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="实验室督导巡查任务创建" center :visible.sync="taskOpen" width="450px" @close="close" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="督导巡查" prop="instanceId">
            <el-select v-model="form.instanceId" placeholder="请选择督导巡查" size="small" style="width: 300px;">
              <el-option v-for="item in checkList" :key="item.instanceId" :label="item.flowName"
                :value="item.instanceId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目标机构" prop="orgId">
            <treeselect v-model="form.orgId" :options="deptOptions" :default-expand-level="2"
            :show-count="true" placeholder="请选择机构" style="width: 300px;"
              size="small" />
          </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitTaskForm">生成任务</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
	</div>
</template>
<script>
import FlowChoose from '@/views/labdata/flowDefination/flow_choose.vue'
import { getCheckedList } from "@/api/labdata/employee"
import { getCheckedList as getOrgCheckedList, treeselect} from "@/api/labdata/orgnization"
import AddTab from "./addTab.vue"
import ItemTab from "./itemTab.vue"
import { formatCurrentDate } from "@/utils/index";
import { getTask, submitTask,listImmediatelyCheck,generalCheckTask  } from "@/api/labsafety/task";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "CreateDialog",
  components: { AddTab, ItemTab, FlowChoose,Treeselect },
  data() {
    return {
      title: '南京邮电大学实验室检查',
      activeName: 'first',
      open: false,
      taskOpen: false,
      checkList:[],
      deptOptions:[],
      form: {},
      rules:{
        orgId: [{
          required: true,
          message: "目标机构必须选择",
          trigger: "change"
        }],
        instanceId: [{
          required: true,
          message: "督导巡查必须选择",
          trigger: "change"
        }],
      },
    };
  },
  created() {
      this.getTreeselect();
      this.getCheckList();
  },
  methods: {
     /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    getCheckList(){
      listImmediatelyCheck().then(response => {
          this.checkList = response.data;
      });
    },
    setWorkflow(workflowId){
      this.form.workflowId = workflowId;
    },
    submitWorkflow(){
		if(!this.form.workflowId){
		  this.form.workflowId = this.$refs["workFlow"].getWorkflowId();
		}
        submitTask(this.form).then(response => {
          this.msgSuccess("提交成功");
          this.$emit("refresh");
          this.open = false;
        });
    },
    updateForm(form) {
      this.form = form
      getTask(form.instanceId).then(response => {
        this.form = response.data;
        this.$emit("refresh");
      })
      if(this.activeName=='first'){
        this.activeName = 'second';
      }else{
        this.activeName = 'third';
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      //this.activeName = 'first'
      this.taskOpen = true;
      //this.form.createBy = this.$store.state.user.name
      //this.form.createTime = formatCurrentDate()
    },
    submitTaskForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          generalCheckTask(this.form).then(response => {
              this.msgSuccess("督导任务生成成功");
              this.taskOpen = false;
              this.$emit("refresh");
            });
        }
      });

    },
    /** 更新按钮操作 */
    handleUpdate(id) {
      this.reset()
      this.activeName = 'first'
      this.open = true;
      getTask(id).then(response => {
        this.form = response.data;
        if (this.form.copyToUsers) {
          getCheckedList({checkedUserIds: this.form.copyToUsers}).then(res=>{
            this.$refs["addTab"].copyToUsers = res.data
          });
        } else {
            this.$refs["addTab"].copyToUsers = []
        }
        if (this.form.checkUsers) {
          getCheckedList({checkedUserIds: this.form.checkUsers}).then(res=>{
            this.$refs["addTab"].checkUsers = res.data
          });
        } else {
            this.$refs["addTab"].checkUsers = []
        }
        if (this.form.reviewUsers) {
          getCheckedList({checkedUserIds: this.form.reviewUsers}).then(res=>{
            this.$refs["addTab"].reviewUsers = res.data
          });
        } else {
            this.$refs["addTab"].reviewUsers = []
        }
        if (this.form.checkTargetOrgs) {
          getOrgCheckedList({checkedOrgIds: this.form.checkTargetOrgs}).then(res=>{
            this.$refs["addTab"].checkTargetOrgs = res.data
          });
        } else {
            this.$refs["addTab"].checkTargetOrgs = []
        }
        this.$refs["addTab"].fileList = []
        if (this.form.files) {
            this.$refs["addTab"].fileList = this.form.files
        }

        this.$refs["addTab"].indexItemList = []
        this.$refs["addTab"].selectCheckIndexTable(this.form)
      })
    },
    close() {
      this.activeName = 'first'
      this.form = {};
      this.open = false;
      this.taskOpen = false;
      //this.$refs["addTab"].reset()
    },
    reset() {
      this.form = {}
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["addTab"].submitForm();
      // this.open = false;
    },
    // 取消按钮
    cancel() {
      this.close();
     // this.reset();
    },
  }
};
</script>
<style type="text/css" scoped>
</style>
